<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<title></title>
</head>
<body>
	<input type="file" name="">
	<img src="" alt="待上传">

	<script type="text/javascript">
		let inp = document.querySelector("input")
		let img = document.querySelector("img")

		inp.onchange = function(){
			console.log(inp.files)//上传图片的信息

			// FormData 用来处理文件上传， 通常用来发送表单数据
			let fileParam = new FormData()
			// append 向FormDate里添加一个新的键值对 key：value
			fileParam.append("file", inp.files[0])

			let xhr = new XMLHttpRequest()
			xhr.open("post", "http://47.104.167.34:8107/common/upload")
			xhr.send(fileParam)
			xhr.onreadystatechange = function(){
				if (xhr.readyState == 4 ){
					let res = JSON.parse(xhr.responseText)
					console.log(res)
					// http://localhost:3002/upload/1745302285229.png
					let str = res.url
					img.src = str
				}
			}
		}
	</script>
</body>
</html>